package com.zzz.bookdemo.service;

import com.zzz.bookdemo.dao.BookDao;
import com.zzz.bookdemo.enums.BookStatus;
import com.zzz.bookdemo.mapper.BookInfoMapper;
import com.zzz.bookdemo.model.BookInfo;
import com.zzz.bookdemo.model.PageRequest;
import com.zzz.bookdemo.model.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.awt.print.Book;
import java.util.List;

/*
 * @author zzr
 * @date: 2025/09/16  20:42
 * @description:
 */

@Service
public class BookService {

    @Autowired
    private BookInfoMapper bookInfoMapper;

    /**
     * 添加图书
     * @param bookInfo
     */
    public void addBook(BookInfo bookInfo) {
        bookInfoMapper.insertBook(bookInfo);
    }

    /**
     * 返回图书列表
     * @param pageRequest
     * @return
     */
    public PageResult<BookInfo> getBookListByPage(PageRequest pageRequest) {
        Integer count = bookInfoMapper.count();

        List<BookInfo> books = bookInfoMapper.queryBookListByPage(pageRequest);
        for (BookInfo book : books) {
            book.setStatusCN(BookStatus.getNameByCode(book.getStatus()).getName());
         }
        return new PageResult<>(count, pageRequest,books);
    }

    public BookInfo queryBookById(Integer bookId) {
        return bookInfoMapper.queryBookById(bookId);
    }

    public void updateBook(BookInfo bookInfo) {
        bookInfoMapper.updateBook(bookInfo);
    }

    public Integer deleteBookById(Integer bookId) {
        return bookInfoMapper.deleteBookById(bookId);
    }

    public void batchDeleteBook(List<Integer> ids) {
        bookInfoMapper.batchDeleteBook(ids);
    }
}
